package com.gulimall.product.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.constant.LoginConstant;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.impl.CategoryServiceImpl;
import com.gulimall.product.vo.IndexCatalogVo;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Author Rookie-6688
 * @Description
 * @Create 2021-03-29 16:16
 */
@Controller
public class IndexController {

    @Resource
    private CategoryServiceImpl categoryServiceImpl;

    @RequestMapping({"","/index","/index.html"})
    public String index(Model model, HttpSession httpSession){
        Object obj = httpSession.getAttribute(LoginConstant.LOGIN_USERNAME);
//        if(obj == null){
//            // 没有登陆就跳到登录页
//            return "redirect:http://auth.gulimall.com/login.html";
//        }
        List<CategoryEntity> firstLevelCategory = categoryServiceImpl.list(new QueryWrapper<CategoryEntity>().eq("cat_level", 1));
        model.addAttribute("categoryList",firstLevelCategory);
        return "index";
    }

    /**
     * 获取所有二级三级分类，数据结构是<一级分类ID，List<二级分类与三级分类数据实体类>>
     * @return
     */
    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<Long,List<IndexCatalogVo>> getIndexCatalog(){
        return categoryServiceImpl.getIndexCatalog();
    }
}
